<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.real_name" placeholder="客户姓名" style="width: 130px;margin-top:8px" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-select v-model="listQuery.payway" placeholder="付租方式" class="filter-item" style="width:130px;margin-top:8px">
        <el-option label="付租方式" value="" />
        <el-option
          v-for="item in payway"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        />
      </el-select>
      <el-select v-model="listQuery.shareholder" placeholder="股东" class="filter-item" style="width:130px;margin-top:8px">
        <el-option label="股东" value="" />
        <el-option
          v-for="item in shareholderList"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        />
      </el-select>
      <el-select v-model="listQuery.c_id" placeholder="选择门店" filterable class="filter-item" style="width:150px;margin-top:8px">
        <el-option label="选择门店" value="" />
        <el-option
          v-for="item in company_list"
          :key="item.id"
          :label="item.c_name"
          :value="item.id"
        />
      </el-select>

      <el-select v-model="listQuery.lab" placeholder="选择类型" filterable class="filter-item" style="width:150px;margin-top:8px">
        <el-option label="选择类型" value="" />
        <el-option
          v-for="item in lab_list"
          :key="item.lab"
          :label="item.name"
          :value="item.lab"
        />
      </el-select>

      <onwerCompany ref="owner_company" />
      <el-date-picker v-model="listQuery.date" style="width:150px" format="yyyy-MM-dd" type="datetime" placeholder="开始日期" />
      <el-date-picker v-model="listQuery.date_end" style="width:150px" format="yyyy-MM-dd" type="datetime" placeholder="结束日期" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" style="margin-top:8px" @click="handleFilter">查询</el-button>
      <!-- <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="excelout" style="margin-top:8px" >导出</el-button> -->
      <excelout />

    </div>
    <el-row style="margin-top:20px;font-size:14px;color:#666">
      <el-col :span="8">已收租金：{{ count.all_total_money }}</el-col>

    </el-row>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      size="mini"
      border
      fit
      :row-class-name="tableRowClassName"
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="付租时间" align="center" width="100">
        <template slot-scope="{row}">
          <span>{{ row.update_time }}</span>
        </template>
      </el-table-column>
      <el-table-column label="类型" align="center" width="100">
        <template slot-scope="{row}">
          <span>{{ row.pay_type }}</span>
        </template>
      </el-table-column>
      <el-table-column label="门店" align="center" width="120">
        <template slot-scope="{row}">
          <span>{{ row.company_name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="出资公司" align="center" width="200">
        <template slot-scope="{row}">
          <span>{{ row.owner_company_name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="客户姓名" align="center">
        <template slot-scope="{row}">
          <span>{{ row.real_name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单时间" align="center" width="110px">
        <template slot-scope="{row}">
          <span>{{ row.create_time }}</span>
        </template>
      </el-table-column>
      <el-table-column label="融资金额" align="center">
        <template slot-scope="{row}">
          <span>{{ row.total_price }}</span>
        </template>
      </el-table-column>
      <el-table-column label="分期" align="center" width="50px">
        <template slot-scope="{row}">
          <span>{{ row.total_period }}</span>
        </template>
      </el-table-column>
      <el-table-column label="对应期数" align="center" width="60">
        <template slot-scope="{row}">
          <span>{{ row.qs }}</span>
        </template>
      </el-table-column>
      <el-table-column label="对应日期" align="center" width="100">
        <template slot-scope="{row}">
          <span>{{ row.time_date }}</span>
        </template>
      </el-table-column>
      <el-table-column label="应付金额" align="center" width="80">
        <template slot-scope="{row}">
          <span>{{ row.yf_money }}</span>
        </template>
      </el-table-column>
      <el-table-column label="应付本金" align="center" width="80">
        <template slot-scope="{row}">
          <span>{{ row.benjin }}</span>
        </template>
      </el-table-column>
      <el-table-column label="应付租金收益" align="center" width="80">
        <template slot-scope="{row}">
          <span>{{ row.money_rate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="应付流量费" align="center" width="80">
        <template slot-scope="{row}">
          <span>{{ row.flow }}</span>
        </template>
      </el-table-column>
      <el-table-column label="本次付租金额" align="center">
        <template slot-scope="{row}">
          <span>{{ row.total_money }}</span>
        </template>
      </el-table-column>

      <el-table-column label="付租状态" align="center">
        <template slot-scope="{row}">
          <span>{{ row.hk_name }}</span>
        </template>
      </el-table-column>

      <el-table-column label="付租渠道" align="center" width="100">
        <template slot-scope="{row}">
          <span>{{ row.payname }}</span>
        </template>
      </el-table-column>
      <el-table-column label="记账人" align="center" width="80px">
        <template slot-scope="{row}">
          <span>{{ row.admin_name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否分配" align="center" width="80px">
        <template slot-scope="{row}">
          <span v-if="row.is_settle==0&&row.settle==0">未分配</span>

          <span v-if="row.is_settle==1||row.settle==1">已分配</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" width="180" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button v-if="row.is_settle==0&&row.settle==0&&row.delete_status==0" type="primary" size="mini" @click="settle(row)">
            分配
          </el-button>
          <el-button v-if="nodes.indexOf('145')!=-1&&row.delete_status==0" type="danger" size="mini" @click="cancelDay(row)">
            作废
          </el-button>
          <span v-if="row.delete_status==1">已作废</span>
          <!-- <span v-else>
            已分配
          </span> -->
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="分配信息" :visible.sync="settleAble" style="width:1000px;margin:auto">
      <el-table :data="shareholderList" border highlight-current-row style="width: 100%;">
        <el-table-column label="股东" align="center">
          <template slot-scope="{row}">
            <span>{{ row.shareholdername }}</span>
          </template>
        </el-table-column>
        <el-table-column label="本金" align="center">
          <template slot-scope="{row}">
            <span>{{ row.benjin_percentage }}</span>
          </template>
        </el-table-column>
        <el-table-column label="分利" align="center">
          <template slot-scope="{row}">
            <span>{{ row.all_benefits }}</span>
          </template>
        </el-table-column>
        <el-table-column label="合计" align="center">
          <template slot-scope="{row}">
            <span>{{ row.all_benefits+row.benjin_percentage }}</span>
          </template>
        </el-table-column>
      </el-table>
      <el-form ref="dataForm" :model="settle_form" label-width="120px" style="width: 100%;border: 1px solid #eee;padding-top: 20px;">
        <el-form-item label="分配日期">
          <el-date-picker v-model="settle_form.date" style="width:150px" format="yyyy-MM-dd" type="datetime" placeholder="日期" />
        </el-form-item>
        <el-form-item label="备注信息">
          <el-input v-model="settle_form.desc" type="text" style="width: 80%;" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="shareholderSettleSubmit(3)">确 定</el-button>
      </span>
    </el-dialog>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

  </div>
</template>

<script>
import excelout from '@/components/excelout/index.vue'
import {
  shareholderSettleInfo,
  getFinanceList,
  shareholderSettleSubmit,
  deletePaymentRecord,
  cancelDay,
  getShareHolder,
  getPayWay,
  exceloutday
} from '@/api/finance'
import waves from '@/directive/waves' // waves directive
import { merchantListOnlyMerchant } from '@/api/merchant'
import onwerCompany from '@/components/company/owner_company.vue'
import {
  formatDate
} from '@/utils/formatDate.js'
import {
  getAllList
} from '@/api/user'
import {
  parseTime, getNodes
} from '@/utils'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
export default {
  name: 'ComplexTable',
  components: {
    Pagination, excelout, onwerCompany
  },
  directives: {
    waves
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        0: '禁用',
        1: '启用'
      }
      return statusMap[status]
    },

    formatDate(time) {
      time = time * 1000
      const date = new Date(time)

      return formatDate(date, 'yyyy-MM-dd')
    },
    typeFilter(type) {
      return calendarTypeKeyValue[type]
    }
  },
  data() {
    return {
      count: [],
      shareQuery: {
        limit: 100
      },
      company_list: [],
      settleAble: false,
      shareholderList: {},
      nodes: [],
      settle_form: {

      },
      lab_list: [
        {
          lab: 1,
          name: '租金'
        },
        {
          lab: 2,
          name: '加融'
        }
      ],
      value1: '',
      value2: '',
      tableKey: 0,
      list: null,
      payway: [],
      collector: [],
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10,
        idcard: null,
        mobile: null,
        real_name: null,
        merchant_name: null
      },
      payment_record_list: [],
      dialogFormVisible: false,
      dialogStatus: ''
    }
  },
  created() {
    this.getShareHolder()
    this.getCompanyList()
    this.getPayWay()
    this.cacheQueryFormat()
    this.getList()
    this.nodes = getNodes()
    // 添加钩子，页面刷新
    window.addEventListener('beforeunload', e => this.beforeunloadFn(e))
  },
  mdestroyed() {
    // 添加钩子，页面刷新
    window.removeEventListener('beforeunload', e => this.beforeunloadFn(e))
  },
  methods: {
    getShareHolder(order_id) {
      getShareHolder(this.shareQuery).then(response => {
        this.shareholderList = response.data.data
      })
    },
    getCompanyList() {
      merchantListOnlyMerchant(this.listMerchantQuery).then(response => {
        this.company_list = response.data.company
        this.sale_list = response.data.sale.data
      })
    },
    cancelDay(row) {
      this.$confirm('此操作将删除永久此纪录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const pam = { record_id: row.record_id }
        cancelDay(pam).then(response => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getList()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    beforeunloadFn(e) {
      // 刷新页面事件
      sessionStorage.removeItem('finaceday')
    },
    cacheQueryFormat() {
      const cacheQuery = sessionStorage.getItem('finaceday')
      if (cacheQuery) {
        this.listQuery = JSON.parse(cacheQuery)
      }
    },
    tableRowClassName({ row, rowIndex }) {
      if (row.delete_status == 1) {
        return 'cancel-row'
      }
      return ''
    },
    excelout() {
      this.listLoading = true
      this.listQuery.page = 1
      this.listQuery.limit = this.total
      exceloutday(this.listQuery).then(response => {
        this.listLoading = false
        const filepath = response.data.path
        window.location.href = filepath
      })
    },
    getPayWay() {
      getPayWay().then(res => {
        this.payway = res.data
      })
    },
    getList() {
      sessionStorage.setItem('finaceday', JSON.stringify(this.listQuery))
      this.listLoading = true
      getFinanceList(this.listQuery).then(response => {
        this.list = response.data.data
        this.count = response.data.count
        this.total = response.data.total
        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 0.2 * 1000)
      })
    },
    settle(row) {
      this.settle_form.order_id = row.order_id
      this.settle_form.pay_id = row.pay_id
      this.settle_form.record_id = row.record_id
      this.settleAble = true
      const pam = { order_id: row.order_id, pay_id: row.pay_id, record_id: row.record_id }
      shareholderSettleInfo(pam).then(response => {
        this.shareholderList = response.data
        setTimeout(() => {
          this.listLoading = false
        }, 0.2 * 1000)
      })
    },
    shareholderSettleSubmit() {
      shareholderSettleSubmit(this.settle_form).then(response => {
        if (response.code == 200) {
          this.$notify({
            title: '提示',
            message: response.msg,
            type: 'success',
            duration: 2000,
            onClose: this.getList()
          })
          this.settleAble = false
        } else {
          this.$notify({
            title: '提示',
            message: response.msg,
            type: 'error',
            duration: 2000
          })
        }
      })
    },
    handleFilter() {
      const owner_company_id = this.$refs.owner_company.$data.listQuery.company_id
      this.listQuery.owner_company_id = owner_company_id
      this.getList()
    }
  }
}
</script>
<style>
.el-table .cancel-row {
      background: #ccc;
    }
</style>
